<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/6
 * Time: 14:07
 */
namespace app\api\model;
use app\admin\model\BydBanner;
use app\admin\model\BydDesfollow;
use app\admin\model\BydDesigner;
use app\admin\model\BydDesignerPro;
use app\admin\model\BydHuxin;
use app\admin\model\BydHuxinCategory;
use app\admin\model\BydLoupanCategory;
use app\admin\model\BydMyfollow;
use app\admin\model\BydPl;
use app\admin\model\BydPlhf;
use app\admin\model\BydVideo;
use app\admin\model\BydZan;
use think\Model;

class BydSet extends Model
{
    // 表名
    protected $name = 'byd_set';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
/**
 * 首页-旗舰套系
 */
    public static function indexset($data){
        if($data['setid']!=13){
            $where['qjset_id']=$data['setid'];
            $where['deletetime']=null;
            $arr=BydSet::where($where)->paginate($data['limit']);
        }else{
            $where['deletetime']=null;
            $arr=BydSet::where($where)->order('weigh desc')->paginate($data['limit']);//全部
        }
        if($arr){
            return $arr;
        }
    }
    /**
     * 首页轮播
     */
    public static function banner(){
        $arr=BydBanner::order('weigh desc')->select();
//        if(!empty($arr)){
//            foreach ($arr as $v){
//                if($v['stime']<time() && $v['endtime']>time()){
//                    $arr[]=$v;
//                }
//            }
//        }

        if($arr){
            return $arr;
        }else{
            return null;
        }
    }
    /**
     * 户型攻略
     */
    public static function huxin($data){
        if($data['id']==0){
            $arr=BydHuxin::select();
        }else{
            $arr=BydHuxin::where('huxin_category_id',$data['id'])->select();
        }

        if($arr){
            return $arr;
        }else{
            return null;
        }
    }
    /**
     *户型筛选条件
     */
    public static function huxincategory(){
        $arr=BydHuxinCategory::order('weigh asc')->select();
        if($arr){
            return $arr;
        }else{
            return null;
        }
    }
    /**
     * 佳作分享
     */
    public static function goodpr($data){
        if($data['state']==0 || $data['state']==1){
            $array='0,1';
            $arr=db('BydVideo')->where('state','in',$array)->order('weigh desc')->paginate($data['limit']);
        }else{
            $arr=db('BydVideo')->where('state',$data['state'])->order('weigh desc')->paginate($data['limit']);
        }
        //$arr=BydVideo::where('state',$data['state'])->paginate($data['limit']);
        $video=[];
        if($arr){
            foreach ($arr as  $k =>$v) {
                //$videos=$v['videos'];
//                $video[$k]['video'] = $v['video'];
//                $video[$k]['videos'] = $v['videos'];
//                $v['video'] = $video;
                $v['zans'] = BydZan::where('user_id', $data['user_id'])->where('video_id', $v['id'])->find();
                if ($data['type'] == 1 || $data['type'] == 3) {
                    $v['follow'] = BydMyfollow::where('user_id', $data['user_id'])->where('video_id', $v['id'])->find();
                } else {
                    $v['follow'] = BydMyfollow::where('user_id', $data['user_id'])->where('shop_id', $v['id'])->find();
                }
                $wheres['video_id'] = $v['id'];
//                $pl = db('BydPl')->where($wheres)->order('id desc')->select();//查询出这个帖子的所有评论
//
//                if ($pl) {
//                    //循环每一条评论
//                    foreach ($pl as $key => &$vv) {
//                        $wheree['id'] = $vv['user_id'];
//                        $usfind = db('BydUser')->where($wheree)->field('username,avatar')->find();//找到此条评论的用户新西
//                        $vv['name'] = $usfind['username'];
//                        $vv['head'] = $usfind['avatar'];
//                        if (!@fopen($vv['head'], 'r')) {
//                            $vv['head'] = 'http://bao.scwushen.com' . $vv['head'];//头像
//                        }
//                        $wh['pl_id'] = $vv['id'];
//                        // $wh['pl_user_id'] = $v['user_id'];
//                        $vv['list'] = db('BydPlhf')->where($wh)->order('create_at desc')->select();//找到此条评论的回复
//                        if ($vv['list']) {
////                            $asa=$v['tt'];
//                            foreach ($vv['list'] as &$q) {
//                                $q['bhhfuser'] = db('BydUser')->where('id', $q['pl_user_id'])->field('username,avatar')->find();//找到被回复人的信息
//                                $qqq = db('BydUser')->where('id', $q['user_id'])->field('username,avatar')->find();//找到回复人的信息
//                                $q['user'] = $qqq;
//                            }
//                        }
//                    }
//                    $v['pl'] = $pl;
////                    print_r($v);die;
//                }else{
//                    $v['pl'] = [];
//                }

                $arr[$k] = $v;

            }
            return $arr;
        }else{
            return null;
        }
    }
    /**
     * 点赞/取消点赞
     */
    public static function videozan($data){
        $add=new BydZan();
        if($data['type']==0){

                $add->data([
                    'video_id'  => $data['video_id'],
                    'user_id'  => $data['user_id'],
                    'state'  => $data['state'],
                ]);
            $add->save();
            if($add){
                if($data['state']){
                    $sum=BydVideo::where('id',$data['video_id'])->setInc('zan',1);
                }else{
                    $sum=BydDesigner::where('id',$data['video_id'])->setInc('zan',1);
                }

                return $sum;
            }else{
                return null;
            }
        }else{
            $del=$add->destroy($data['video_id']);
            if($del){
                $dec=BydVideo::where('id',$data['video_id'])->setDec('zan',1);
                return $dec;
            }else{
                return null;
            }
        }

    }
    /**
     * 评论
     * state  0为设计师1为视频
     */
    public static function pl($data)
    {
        if($data['content']==null){
            $creat['image'] = $data['image'];
        }elseif($data['image']==null){
            $creat['content'] = $data['content'];
        }else{
            $creat['image'] = $data['image'];
            $creat['content'] = $data['content'];
        }
        if($data['state']==0){
            $creat['dis_id'] = $data['id'];
        }else{
            $creat['video_id'] = $data['id'];
        }
        $creat['user_id'] = $data['userid'];
        $pl = new BydPl();
        $arr = $pl->create($creat);
        if ($arr) {
            if($data['state']==0){
                $up=BydDesigner::where('id',$data['id'])->setInc('plcount',1);
            }else{
                $up=BydVideo::where('id',$data['id'])->setInc('plnum',1);
            }
            return $arr;
            //$this->success(__('评论成功'), ['status' => $arr]);
        } else {
            return null;
        }
    }

    /**
     *   回复评论
     * @ApiMethod (POST)
     * @param integer $pl_id 评论的id
     * @param integer $pl_user_id 被回复人的id
     * @param integer $userid 用户的id
     * @param varchar $content 评价的内容
     */
    public static function hfpl($data)
    {
        $find=BydUser::where('id',$data['userid'])->find();
        if($find){
            $cerat['pl_id'] = $data['pl_id'];
            $cerat['pl_user_id'] = $data['pl_user_id'];
            $cerat['user_id'] = $data['userid'];
            $cerat['content'] = $data['content'];
            $pl = new BydPlhf();
            $arr = $pl->create($cerat);
            if ($arr) {
                return $arr;
            } else {
                return null;
            }
        }else{
            return false;
        }

    }
    /**
     * 楼盘设计筛选条件
     */
    public static function loupancategory(){
        $arr=BydLoupanCategory::select();
        $qc=array_unique($arr);

        if($qc){
            return $arr;
        }else{
            return null;
        }
    }
    /**
     * 楼盘设计详情
     */
    public static function loupanxq($data){
        $find=BydDesignerPro::find($data['id']);
        if ( empty($find)) {return null;};

        $find['des']=BydDesigner::find($find['des_id']);//设计师
        $find['des']['zans']=BydZan::where('user_id',$data['user_id'])->where('video_id',$find['des_id'])->where('state',1)->find();
        $follows=BydDesfollow::where('text_id',$find['des_id'])->where('user_id',$data['user_id'])->find();
        if($follows){
            $find['des']['follows']=0;
        }else{
            $find['des']['follows']=1;
        }

        if($find){
            return $find;
        }else{
            return  null;
        }
    }
}